package com.hzy.seataorder.order.service.impl;

import com.hzy.common.entities.CommonResult;
import com.hzy.order.entities.Order;
import com.hzy.seataorder.order.dao.OrderMapper;
import com.hzy.seataorder.order.service.AccountService;
import com.hzy.seataorder.order.service.OrderService;
import com.hzy.seataorder.order.service.StorageService;
import com.hzy.storage.entities.Storage;
import com.hzy.utils.UUIDUtils;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 这里填写描述
 * 填写修改说明
 *
 * @author hzy
 * @version Revision:v1.0
 * @since Date:2022-03-15 15:33
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    @Override
    public int create(Order order) {
        order.setId(UUIDUtils.generate());
        log.info("----------> 创建订单开始");
        int result = orderMapper.insert(order);
        log.info("----------> 创建订单结束:{}, :{}", order, result);

        log.info("----------> 扣减库存开始");
        CommonResult<Order> orderResult = storageService.decrease(order.getCommodityCode(), order.getCount());
        log.info("----------> 扣减库存结束:{}", orderResult);


        log.info("----------> 扣减账户开始");
        CommonResult<Storage> storageResult = accountService.decrease(order.getUserId(), order.getMoney());
        log.info("----------> 扣减账户结束:{}", storageResult);

        order.setStatus(1);
        orderMapper.updateStatus(order);
        log.info("----------> 下单结束");

        return result;
    }


}
